package com.lorizz.model;

import java.lang.reflect.ParameterizedType;
import java.util.Set;

public abstract class AbstractRepositoryManager {

    private Set<Repository<?, ?>> managedRepositories;

    public void setManagedRepositories(Set<Repository<?, ?>> managedRepositories) {
        this.managedRepositories = managedRepositories;
    }

    protected Repository<?, ?> getRepository(Class<?> entityClazz) {
        if (managedRepositories == null)
            return null;
        for (Repository<?, ?> repo : managedRepositories) {
            Class<?> clazz = (Class<?>) ((ParameterizedType) repo.getClass()
                    .getGenericSuperclass()).getActualTypeArguments()[0];
            if (entityClazz.getSimpleName().equals(clazz.getSimpleName())) {
                return repo;
            }
        }
        return null;
    }

}
